<?php

use core\event\EventMessage;
use core\lib\BotAPI;
use core\lib\Config;
use core\lib\Logger;
use core\lib\MessageBuild;

/**
 * 获得日志实例
 * @return \Monolog\Logger
 */
function getLogger(): \Monolog\Logger {
    return Logger::getInstance();
}

/**
 * 获得配置项
 * @param string $key
 * @return mixed
 */
function config($key = '') {
    return Config::get($key);
}

/**
 * 发送艾特某人消息
 * @param int $user_id
 * @return string
 */
function at(int $user_id = null): string {
    return MessageBuild::at($user_id);
}

/**
 * 发送图片消息
 * @param string $uri
 * @return string
 */
function image(string $uri): string {
    return MessageBuild::image($uri);
}

/**
 * 获得HTTP输入，同时支持JSON
 * @param string $key
 * @return array|mixed
 */
function input(string $key = null) {
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        $from = $_GET;
    } else {
        $json = json_decode(file_get_contents('php://input'), true);
        if (!empty($json)) {
            $from = $json;
        } else {
            $from = $_POST;
        }
    }
    if (is_null($key)) {
        return $from;
    }
    return $from[$key];
}

/**
 * 发送HTTP响应
 * @param $status
 * @param $message
 * @param $data
 */
function http_send($status, $message = '', $data = null) {
    header('Content-Type', 'application/json');
    echo json_encode([
        'status' => $status,
        'message' => $message,
        'data' => $data,
    ], JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
}

function contains($str, ...$substr) {
    foreach ($substr as $_sub) {
        if (strpos($str, $_sub) !== false) {
            return true;
        }
    }
    return false;
}

function bot() {
    return (new BotAPI());
}
